using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using KY.Mongo;

namespace KY.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.UseKYMongo();


            #region Swagger普通注册
            //服务 swagger普通注册
            services.AddSwaggerGen(option =>
            {
                option.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "My API V1",
                    Description = "API for My API V1",
                    Contact = new OpenApiContact() { Name = "My API", Email = "zhuxiong@kzspp.com" },
                   
                });

                // include document file
                option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{typeof(Startup).Assembly.GetName().Name}.xml"), true);
            });
            #endregion


            #region 添加Bearer的swagger
            //        services.AddSwaggerGen(option =>
            //        {
            //            // ...

            //            // Add security definitions
            //            option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
            //            {
            //                Description = "Please enter into field the word 'Bearer' followed by a space and the JWT value",
            //                Name = "Authorization",
            //                In = ParameterLocation.Header,
            //                Type = SecuritySchemeType.ApiKey,
            //            });
            //            option.AddSecurityRequirement(new OpenApiSecurityRequirement
            //{
            //    { new OpenApiSecurityScheme
            //    {
            //        Reference = new OpenApiReference()
            //        {
            //            Id = "Bearer",
            //            Type = ReferenceType.SecurityScheme
            //        }
            //    }, Array.Empty<string>() }
            //});
            //        });
            #endregion


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            ////启用中间件服务生成Swagger作为JSON终结点
            //app.UseSwagger();
            ////启用中间件服务对swagger-ui，指定Swagger JSON终结点
            //app.UseSwaggerUI(c =>
            //{
            //    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            //});

            //Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
            //Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint
            app.UseSwaggerUI(option =>
            {
                option.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

                option.RoutePrefix = string.Empty;
                option.DocumentTitle = "My API V1";
            });


            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                //    endpoints.MapControllerRoute(name: "autoset",
                //pattern: "autoset/{*a}/{*b}",
                //defaults: new { controller = "Autoset", action = "Index" });

                endpoints.MapControllers();
            });
        }
    }
}
